package com.radar.common;

import cn.hutool.core.exceptions.ExceptionUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
public class RadarSite {

    public static RadarSite radarSite;

    private List<RadarSiteItem> radars;

    public Optional<RadarSiteItem> getItem(String station) {
        return this.radars.stream().filter(z -> z.getStation().equals(station + "")).findFirst();
    }
    public static String UNDEF = "UNDEF";

    @Data
    public class RadarSiteItem {
        private String stationName;
        private String station;
        private String province;
        private String city;
        private CinradType type;
        private double lon;
        private double lat;
        private double alt;
        private int range = -1;

        public RadarSiteItem(String stationName, String station, String province, String city, CinradType type, double lon, double lat, double alt) {
            this.stationName = stationName;
            this.station = station;
            this.province = province;
            this.city = city;
            this.type = type;
            this.lon = lon;
            this.lat = lat;
            this.alt = alt;
        }

        @Override
        public String toString() {
            return "RadarSiteItem{" +
                    "stationName='" + stationName + '\'' +
                    ", station='" + station + '\'' +
                    ", type=" + type +
                    ", lon=" + lon +
                    ", lat=" + lat +
                    ", alt=" + alt +
                    '}';
        }
    }

    public static RadarSite getInstance() {
        if (radarSite == null) {
            radarSite = new RadarSite();
            radarSite.radars = new ArrayList<>();
            try (InputStream stream = new RadarSite().getClass().getClassLoader().getResourceAsStream("radarsite_pup.conf");
                 BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"))) {
                int index = 0;
                String len = null;
                while ((len = br.readLine()) != null) {
                    if (index != 0) {
                        String[] split = len.split("_");
                        try {
                            double lon = Dms2D(split[5] + "°" + split[6] + "'" + split[7] + "\"");
                            double lat = Dms2D(split[8] + "°" + split[9] + "'" + split[10] + "\"");
                            double alt = Double.valueOf(split[11]);
                            RadarSiteItem radarSiteItem = radarSite.new RadarSiteItem(
                                    split[0],
                                    split[1],
                                    split[3],
                                    split[2],
                                    CinradType.valueOf(split[4]),
                                    lon,
                                    lat,
                                    alt);
                            if (split.length == 13) {
                                radarSiteItem.range = Integer.parseInt(split[12]);
                            }
                            radarSite.radars.add(radarSiteItem);
                        } catch (Exception e) {
                            log.error("init radarsite error {}", len);
                        }
                    }
                    index++;
                }
            } catch (IOException e) {
                log.error(ExceptionUtil.stacktraceToString(e));
            }
        }
        return radarSite;
    }

    /**
     * 经纬度转换 ，度分秒转度
     *
     * @param jwd
     * @return
     * @author
     */
    public static double Dms2D(String jwd) {
        if (jwd != null && !jwd.equals("") && (jwd.contains("°"))) {//如果不为空并且存在度单位
            //计算前进行数据处理
            jwd = jwd.replace("E", "").replace("N", "").replace(":", "").replace("：", "");
            double d = 0, m = 0, s = 0;
            d = Double.parseDouble(jwd.split("°")[0]);
            //不同单位的分，可扩展
            if (jwd.contains("′")) {//正常的′
                m = Double.parseDouble(jwd.split("°")[1].split("′")[0]);
            } else if (jwd.contains("'")) {//特殊的'
                m = Double.parseDouble(jwd.split("°")[1].split("'")[0]);
            }
            //不同单位的秒，可扩展
            if (jwd.contains("″")) {//正常的″
                //有时候没有分 如：112°10.25″
                s = jwd.contains("′") ? Double.parseDouble(jwd.split("′")[1].split("″")[0]) : Double.parseDouble(jwd.split("°")[1].split("″")[0]);
            } else if (jwd.contains("''")) {//特殊的''
                //有时候没有分 如：112°10.25''
                s = jwd.contains("'") ? Double.parseDouble(jwd.split("'")[1].split("''")[0]) : Double.parseDouble(jwd.split("°")[1].split("''")[0]);
            }
            jwd = String.valueOf(d + m / 60 + s / 60 / 60);//计算并转换为string
            //使用BigDecimal进行加减乘除
        }
        return Double.parseDouble(jwd);
    }

}
